package com.alibaba.citrus.ots.sales.plan.converter;

import com.alibaba.citrus.ots.sales.plan.model.features.SalesPlanLineFeatures;
import com.alibaba.citrus.ots.sales.plan.sdo.SalesPlanLineSDO;
import com.alibaba.cz.base.tool.StringUtil;
import com.epoch.app.bcots.sales_plan.salesplandetail.model.SalesPlanDetail;
import com.google.common.collect.Lists;
import net.sf.json.JSONObject;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author luyi
 * @date 2021/10/26 10:49 下午
 */
public class SalesPlanLineConverter {

    public static SalesPlanLineSDO planLineDoToSdo(SalesPlanDetail source){
        if (Objects.isNull(source)){
            return null;
        }
        SalesPlanLineSDO target = SalesPlanLineSDO.builder().build();
        BeanUtils.copyProperties(source,target);
        if (StringUtil.isNotEmpty(source.getFeatures())){
            SalesPlanLineFeatures salesPlanLineFeatures = (SalesPlanLineFeatures) JSONObject.toBean(JSONObject.fromObject(source.getFeatures()), SalesPlanLineFeatures.class);
            target.setSalesPlanLineFeatures(salesPlanLineFeatures);
        }
        return target;
    }

    public static SalesPlanDetail planLineSdoToDo(SalesPlanLineSDO source){
        if (Objects.isNull(source)){
            return null;
        }
        SalesPlanDetail target = new SalesPlanDetail();
        BeanUtils.copyProperties(source,target);
        if (source.getSalesPlanLineFeatures() != null){
            target.setFeatures(JSONObject.fromObject(source.getSalesPlanLineFeatures()).toString());
        }
        return target;
    }


    public static List<SalesPlanDetail> planLineSdoToDoList(List<SalesPlanLineSDO> sourceList){
        if (CollectionUtils.isEmpty(sourceList)){
            return null;
        }
        List<SalesPlanDetail> targetList = Lists.newArrayListWithCapacity(sourceList.size());
        for (SalesPlanLineSDO planLine : sourceList) {
            targetList.add(planLineSdoToDo(planLine));
        }
        return targetList;
    }
}
